<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div style="margin-left: 10px;">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="物料编码" prop="goodsCode">
          <el-input v-model="queryParams.goodsCode" placeholder="请输入物料编码"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="物料名称" prop="goodsName">
          <el-input v-model="queryParams.goodsName" placeholder="请输入物料名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="物料型号" prop="goodModel">
          <el-input v-model="queryParams.goodModel" placeholder="请输入物料型号"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <!--          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddByIron()" v-if="ironDetailId"-->
          <!--          >选择物料(铁件)-->
          <!--          </el-button>-->
        </el-form-item>
      </el-form>
      <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
                :max-height="tableMaxHeight" @sort-change="handleSortChange">
        <el-table-column type="index" label="序号"></el-table-column>

        <el-table-column prop="goodsCode" label="物料选择" width="380">
          <template slot-scope="scope">
            <el-autocomplete style="width: 350px;"
                             v-model="scope.row.goodsBrand"
                             :fetch-suggestions="querySearch"
                             placeholder="请输入内容"
            >
              <template #default="{ item, index }">
                <div @click="handleClick(item, scope.$index)">
                  {{ item.value }}
                </div>
              </template>
            </el-autocomplete>
          </template>
        </el-table-column>

        <el-table-column prop="goodsCode" label="存货编码">
          <template slot-scope="scope">
            <el-input v-model="scope.row.goodsCode"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="goodsName" label="物料名称">
          <template slot-scope="scope">
            <el-input v-model="scope.row.goodsName"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="goodModel" label="物料型号">
          <template slot-scope="scope">
            <el-input v-model="scope.row.goodModel"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="goodsUnit" label="计量单位">
          <template slot-scope="scope">
            <el-input v-model="scope.row.goodsUnit"></el-input>
          </template>
        </el-table-column>

        <el-table-column prop="goodsCount" label="物料数量" width="150">
          <template slot-scope="scope">
            <el-input-number size="mini" v-model="scope.row.goodsCount"></el-input-number>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template slot-scope="scope">
            <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.$index)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                         v-hasPermi="['develop:GoodsSaleMaterial:remove']">删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>

      </el-table>
      <el-row>
        <el-col :span="24" style="text-align: center;padding-top: 20px;">
          <el-button style="width: 100%;" icon="el-icon-plus" plain type="primary" @click="addRow">添加行</el-button>
        </el-col>
      </el-row>
      <GoodsSaleMaterialCount-form ref="form" @ok="getList"></GoodsSaleMaterialCount-form>
      <goodsOrderInForm-list ref="goodsOrderinform" @ok="getList"></goodsOrderInForm-list>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getAction, addAction, deleteAction, updateAction, postAction} from '@/api/manage'
import GoodsOrderInFormList from '@/views/develop/projectBaseInfo/prjForGoodsIn/GoodsOrderInFormList'
import GoodsSaleMaterialCountForm from '@/views/develop/projectBaseInfo/prjForGoodsIn/GoodsSaleMaterialCountForm'

export default {
  name: "IronBomFormList",
  components: {
    GoodsOrderInFormList,
    GoodsSaleMaterialCountForm
  },
  data() {
    return {
      title: "1产品物料列表",
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      rows: [],
      rules: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售信息管理表格数据
      dataList: [],
      columns: [],
      goodsId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20000,
        goodsCode: null,
        goodsName: null,
        goodModel: null,
        goodsBrand: null,
        gsaleId: null,
        code: null,
        name: null,
        params: {},
      },
      goodsQueryParams:{
        pageNum: 1,
        pageSize: 20,
        model: null,
      },
      goodsList: [],
      url: {
        list: '/goodsSaleMaterial/goodsSaleMaterial/list',
        addForIronList: '/goodsSaleMaterial/goodsSaleMaterial/addForIronList',
        remove: '/goodsSaleMaterial/goodsSaleMaterial/',
      },
      tableMaxHeight: window.innerHeight - 320,
      gsaleId: null,
      ironDetailId: null,
      form:{}
    };
  },
  mounted() {
  },
  created() {
    window.addEventListener('resize', this.getTableHeight);
    this.getGoodsList();
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight);
  },
  methods: {


    getGoodsList() {
      let params = {
        "pageSizeZero": true
      };
      getAction('/goodsOrderIn/goodsOrderIn/listCache', params).then(response => {
        this.goodsList = response.rows;
        for (let i = 0; i < this.goodsList.length; i++) {
          this.goodsList[i].value = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
        }
      })
    },


    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAddByGoods() {
      this.$refs.goodsOrderinform.showOrderInByGoods(this.gsaleId);
    },

    handleAddByIron() {
      this.$refs.goodsOrderinform.showOrderInByIron(this.ironDetailId);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    chaijieGoods(row) {
      this.queryParams.gsaleId = row.id;
      this.gsaleId = row.id;
      this.open = true;
      this.getList();
    },

    bomByIron(row) {
      this.queryParams.ironDetailId = row.id;
      this.ironDetailId = row.id;
      this.open = true;
      this.getList();
    },

    /** 查询销售信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        if (this.dataList.length === 0) {
          for (let i = 0; i < 10; i++) {
            this.dataList.push({
              goodsId: '',//产品编号
              goodsCode: '',//产品编号
              goodsName: '',//名称
              goodModel: '',
              goodsCount: 0
            })
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    //点击按钮添加空行
    addRow() {
      this.$nextTick(() => {
        this.dataList.push({
          goodsCode: '',//产品编号
          goodsName: '',//名称
          goodModel: ''
        })
      })
    },

    querySearch(queryString, cb) {
      let that = this;
      that.goodsQueryParams.model= queryString;

      getAction('/goodsOrderIn/goodsOrderIn/list', that.goodsQueryParams).then(response => {
        that.goodsList = response.rows;
        for (let i = 0; i < this.goodsList.length; i++) {
          that.goodsList[i].value = that.goodsList[i].name + '-' + that.goodsList[i].model + '-' + that.goodsList[i].code;
        }
        const goodsList = that.goodsList;
        const results = queryString
          ? goodsList.filter(that.createFilter(queryString))
          : goodsList;
        // 调用 callback 返回建议列表的数据
        cb(results);
      })
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().includes(queryString.toLowerCase())
        );
      };
    },

    handleClick(item, index) {
      this.$nextTick(() => {
        let newItem = {
          goodsId: item.id,
          goodsCode: item.code, //产品编号
          goodsName: item.name, //名称
          goodModel: item.model,//型号
          goodsUnit: item.unit, // 单位
          goodsBrand: item.brand, // 单位
          goodsCount: "0"
        }
        this.$set(this.dataList, index, newItem)
      })
    },

    //删除行
    handleDelete(index) {
      this.$nextTick(() => {
        this.dataList.splice(index, 1)
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.form.ironDetailId = this.ironDetailId;
      this.form.materialColumns = this.dataList;
      addAction(this.url.addForIronList, this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.$emit('ok');
      });
    },

  }
};
</script>
